var orderId = $Util.getQueryString(document.location.search, "id");
$(function(){
	$(".mc").css("display","none");
	$('#categorys').hover(function(){
		$(this).children('.mc').toggle();
	});
	$Base.isLogin(location.href, getOrder);
	$('#ORDER_ID').val(orderId);
});

function getOrder(){
	var url = '/json?action=GET_T_ORDER_BY_MEMBER_ACTION&ORDER_ID=' + orderId;
	$.post(url, {}, function(data){
		if(data.success){
			var obj = data.root.T_ORDER.rs[0];
			var order = obj.ORDER;
			var detail = obj.DETAIL;
			$.each(detail, function(i, o){
				var productList = '';
				productList += '<ul class="rate-list">';
				productList += '	<li class="rate-box st-show-msg-box">';
				productList += '		<div class="item-rate-info">';
				productList += '			<div class="item-detail">';
				productList += '				<a href="../product/file.html?id='+o.PRODUCT_ID+'" target="_blank" class="item-d-img"><img width="110" height="110" src="../upload/product/'+o.PRODUCT_NO+'.jpg" /></a>';
				productList += '				<div class="item-title">';
				productList += '					<a href="../product/file.html?id='+o.PRODUCT_ID+'" target="_blank" class="blue">'+o.PRODUCT_NAME+'</a>';
				productList += '				</div>';
				productList += '			</div>';
				productList += '		</div>';
				productList += '		<div class="item-rate-main">';
				productList += '			<div id="s_'+o.PRODUCT_ID+'">';
				productList += '				<div id="star" style="margin-left:0px; width:700px;">';
				productList += '			    	<span>感谢您的购买，分享下您的感受吧：</span>';
				productList += '			    	<ul>';
				productList += '			        	<li><a href="javascript:;">1</a></li>';
				productList += '			        	<li><a href="javascript:;">2</a></li>';
				productList += '			        	<li><a href="javascript:;">3</a></li>';
				productList += '			        	<li><a href="javascript:;">4</a></li>';
				productList += '			        	<li><a href="javascript:;">5</a></li>';
				productList += '			    	</ul>';
				productList += '			    	<input type="hidden" name="PRODUCT_ID" value="'+o.PRODUCT_ID+'"/>';
				productList += '			    	<input type="hidden" id="score_'+o.PRODUCT_ID+'" name="SCORE" value="0"/>';
				productList += '			    	<span></span>';
				productList += '			    	<p></p>';
				productList += '				</div>';
				productList += '			</div>';
				productList += '			<div class="rate-msg-box">';
				productList += '				<textarea class="rate-msg" style="font-size:12px; color:#999;" name="COMMENT_TEXT" onkeyup="return countWord(this, '+o.PRODUCT_ID+');" onkeydown="return countWord(this, '+o.PRODUCT_ID+');"></textarea>';
				productList += '			</div>';
				productList += '			<div style="margin-top:5px;">您还可以输入<strong class="r-t-counter" id="f_'+o.PRODUCT_ID+'">300</strong>个字</div>';
				productList += '		</div>';
				productList += '	</li>';
				productList += '</ul>'
				$('#product_list').append(productList);
				loadStar(o.PRODUCT_ID);
			});
		}
	},'json');
}

function subFrm(){
	$('#err_tip').hide();
	var score = $('input[name="SCORE"]');
	var index = 0;
	for(var i = 0; i < score.length; i++){
		if(score[i].value == 0){
			index++;
		}
		if(index == score.length){
			$('#err_tip').show();
			$('#err_msg').html('您还没有对任何商品评分，只有评过分了才能提交哦！');
			return;
		} else if(index > 0 && i == score.length - 1){
			$('#err_tip').show();
			$('#err_msg').html('还有些商品没有评分，只有全部评过分了才能提交哦！');
			return;
		}
	}
	
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		window.history.back();
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

var count = 300;
function countWord(obj, id){
	var text = $.trim($(obj).val());
	
	var e = e || event;
	var currKey = e.keyCode||e.which||e.charCode;
	if(currKey == 8 || currKey == 46){
		if(count - text.length <= 0){
			$('#f_'+id).html('0');
		}
		else{
			$('#f_'+id).html(count - text.length);
		}
		return true;
	}
	
	if(count - text.length <= 0){
		$('#f_'+id).html('0');
		return false;
	}
	$('#f_'+id).html(count - text.length);
	return true;
}

function loadStar(id){
    var oStar = document.getElementById("s_"+ id);
	var aLi = oStar.getElementsByTagName("li");
	var oUl = oStar.getElementsByTagName("ul")[0];
	var oSpan = oStar.getElementsByTagName("span")[1];
	var oP = oStar.getElementsByTagName("p")[0];
	var i = iScore = iStar = 0;
	var aMsg = [
			"很不满意|差得太离谱，与描述的严重不符，非常不满",
			"不满意|部分有破损，与描述的不符，不满意",
			"一般|质量一般，没有描述的那么好", 
			"满意|质量不错，与描述的基本一致，还是挺满意的",
			"非常满意|质量非常好，与描述的完全一致，非常满意"];

	for (i = 1; i <= aLi.length; i++) {
		aLi[i - 1].index = i;
		//鼠标移过显示分数
		aLi[i - 1].onmouseover = function() {
			fnPoint(this.index);
			//浮动层显示
			oP.style.display = "block";
			//计算浮动层位置
			oP.style.left = oUl.offsetLeft + this.index * this.offsetWidth - 104 + "px";
			//匹配浮动层文字内容
			oP.innerHTML = "<em><b>" + this.index + "</b> 分 "+ aMsg[this.index - 1].match(/(.+)\|/)[1] + "</em>"+ aMsg[this.index - 1].match(/\|(.+)/)[1];
		};
		//鼠标离开后恢复上次评分
		aLi[i - 1].onmouseout = function() {
			fnPoint();
			//关闭浮动层
			oP.style.display = "none";
		};
		//点击后进行评分处理
		aLi[i - 1].onclick = function() {
			iStar = this.index;
			$('#score_' + id).val(this.index);
			oP.style.display = "none";
			oSpan.innerHTML = "<strong>" + (this.index) + " 分</strong> ("+ aMsg[this.index - 1].match(/\|(.+)/)[1] + ")";
		}
		//评分处理
		function fnPoint(iArg) {
			//分数赋值
			var iStar = $('#score_' + id).val();
			iScore = iArg || iStar;
			for (i = 0; i < aLi.length; i++){
				aLi[i].className = i < iScore ? "on" : "";
			}
		}
	}
}